class Component < ActiveRecord::Base
  def self.update_by_title(hash)
    c = self.find_by_title(hash[:title])
    tid = hash.delete(:template)
    hash[:resource] = Template.find(tid - 11).name
    if c
      puts [hash[:title], hash[:resource]].inspect
      c.update_attributes(hash)
    else
      self.create(hash)
    end
  end

  def js_hash= (hash)
    self.title = hash[:title]
    self.resource = hash[:resource]
    self.body = hash[:body]
    self.order = hash[:index]
  end

  def jsonize
    {
      :id       => id,
      :title    => title || "新页面",
      :resource => resource,
      :order    => order,
      :body     => body
    }
  end
end
